package com.dingwen.treasure.mybatisplus.util;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dingwen.treasure.base.pojo.vo.PageVo;
import com.dingwen.treasure.base.pojo.vo.Result;
import com.dingwen.treasure.base.pojo.vo.ResultGenerator;
import lombok.experimental.UtilityClass;

/**
 * 结果生成工具
 *
 * @author dingwen
 * @date 2022/11/17
 */
@UtilityClass
public class ResultUtil {

    /**
     * 构建分页结果
     *
     * @param iPage 页面
     * @return {@link Result}<{@link PageVo}<{@link V}>>
     */
    public <V> Result<PageVo<V>> genPageResult(IPage<V> iPage) {
        PageVo<V> pageVo = PageVo.<V>builder()
                .rows(iPage.getRecords())
                .pageSize(Convert.toInt(iPage.getSize()))
                .pageNum(Convert.toInt(iPage.getCurrent()))
                .totalNum(iPage.getTotal())
                .build();
        return ResultGenerator.genSuccessResult(pageVo);
    }
}
